package com.lh_travel.lohas.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lh_travel.lohas.R;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by aaron on 2018/6/7.
 */

public abstract class BaseFragment extends Fragment {

    private boolean isVisible = false;//当前Fragment是否可见
    private boolean isInitView = false;//是否与View建立起映射关系
    private boolean isFirstLoad = true;//是否是第一次加载数据

    public View convertView;
    private SparseArray<View> mViews;
    public MainApplication mapp;
    public Context context;
    public Activity activity;
    public AlertDialog dialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (convertView == null) {
            mapp = (MainApplication) getActivity().getApplication();
            context = getContext();
            activity = getActivity();

            convertView = inflater.inflate(getLayoutId(), container, false);
            mViews = new SparseArray<>();
            initView();
            isInitView = true;
            lazyLoadData();
        }
        return convertView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);


    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {

        if (isVisibleToUser) {
            isVisible = true;
            lazyLoadData();

        } else {
            isVisible = false;
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    private void lazyLoadData() {
        if (isFirstLoad) {

            Log.v("fragment", "第一次加载" + this.getClass().getSimpleName());
        } else {
            Log.v("fragment", "不是第一次加载" + this.getClass().getSimpleName());
        }
        if (!isFirstLoad || !isVisible || !isInitView) {

            return;
        }

        //满足是第一次加载,是可见,是初始化才运行下面代码
        initData();
        isFirstLoad = false;
    }


    /**
     * 加载页面布局文件
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 让布局中的view与fragment中的变量建立起映射
     */
    protected abstract void initView();

    /**
     * 加载要显示的数据
     */
    protected abstract void initData();

    /**
     * fragment中可以通过这个方法直接找到需要的view，而不需要进行类型强转
     *
     * @param viewId
     * @param <E>
     * @return
     */
    protected <E extends View> E findView(int viewId) {
        if (convertView != null) {
            E view = (E) mViews.get(viewId);
            if (view == null) {
                view = (E) convertView.findViewById(viewId);
                mViews.put(viewId, view);
            }
            return view;
        }
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public AlertDialog showMyDialog(String title, String text, String confirm, String cancle, View.OnClickListener confirmListener) {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        View view = View.inflate(context, R.layout.dialog_tos, null);

        builder.setView(view);

        if (title != null && title.length() > 0) {
            TextView tv_title = view.findViewById(R.id.tv_title);
            tv_title.setText(title);
        }

        if (text != null && text.length() > 0) {
            TextView tv_content = view.findViewById(R.id.tv_content);
            tv_content.setText(text);
        }

        RelativeLayout rl_button = view.findViewById(R.id.rl_button);
        Button btn_cancle = view.findViewById(R.id.btn_cancle);
        Button btn_confirm = view.findViewById(R.id.btn_confirm);

        if (confirm != null && confirm.length() > 0) {
            btn_confirm.setText(confirm);

            btn_confirm.setOnClickListener(confirmListener);

        } else {
            btn_confirm.setVisibility(View.GONE);
        }

        if (cancle != null && cancle.length() > 0) {
            btn_cancle.setText(cancle);

            btn_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

        } else {
            btn_cancle.setVisibility(View.GONE);
        }

        if ((confirm == null || confirm.length() == 0) && (cancle == null || cancle.length() == 0)) {
            rl_button.setVisibility(View.GONE);
        }
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();

        return dialog;
    }

}
